<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserService
 *
 * @author admin
 */
class Service_UserService {
    
    public function __construct()
    {

    }

    /**
     * @return Ahs_VO_User
     */
    
    public function getUser($id)
    {
        $userMapper = new Application_Model_UserMapper();
        $user = $userMapper->fetchById($id);

        $userVO = $this->_toUserVO($user);

        return $userVO;
    }
    
    public function addUser($email, $pass, $givenname, $familyname, $role)
    {
        $userMapper = new Application_Model_UserMapper();
        if($userMapper->addUser($email, sha1($pass), $givenname, $familyname, $role))
        {
            return true;
        }
        else{
            return false;
        }
    } 
    
    public function deleteUserFromProject($userid, $projectid)
    {
        $userMapper = new Application_Model_UserMapper();
        if($userMapper->deleteUserFromProject($userid, $projectid))
        {
            return true;
        }
        else{
            return false;
        }
    }

    public function getUsers()
    {   
        $userMapper = new Application_Model_UserMapper();
        $users = $userMapper->fetchAll();

        $userVOs = $this->_toUserVOs($users);

        return $userVOs;
    }

    public function getUsersList()
    {
        return $this->getUsers();
    }
    
    public function getUsersByProjectId($projectid)
    {
        $userMapper = new Application_Model_UserMapper();
        $users = $userMapper->fetchByProjectId($projectid);

        $userVOs = $this->_toUserVOs($users);

        return $userVOs;
    }
    
    public function getUsersBySearchstring($string)
    {
        $userMapper = new Application_Model_UserMapper();
        $users = $userMapper->fetchBySearchstring($string);

        $userVOs = $this->_toUserVOs($users);

        return $userVOs;
    }
    
    public function getIdByEmail($email, $pass)
    {
        
        $userMapper = new Application_Model_UserMapper();
        $user = $userMapper->fetchIdByEmail($email, sha1($pass));

        //$userVO = $this->_toUserVO($user);

        //return $userVO;
        
        return $user->Id;
    }

    /**
     * @param Application_Model_User $user
     * @return VO_User
     */
    private function _toUserVO(Application_Model_User $user)
    {
        $userVO = new VO_User();
        $userVO->id             = $user->Id;
        $userVO->email          = $user->Email;
        $userVO->password       = $user->Password;
        $userVO->givenname      = $user->Givenname;
        $userVO->familyname     = $user->Familyname;
        $userVO->active         = $user->Active;
        $userVO->role           = $user->Role;
        $userVO->creationdate   = $user->Creationdate;
        $userVO->deleted        = $user->Deleted;
  
        return $userVO;
    }

    /**
     * @param array $users
     * @return array
     */
    private function _toUserVOs($users = array())
    {
        $userVOs = array();
        foreach ($users as $user) {
            $userVOs[] = $this->_toUserVO($user);
        }

        return $userVOs;
    }
    
    public function logOut()
    {
        Zend_Auth::getInstance()->clearIdentity();
    }
}

?>
